package com.jhhc.StormSurgeForecast.AstronomicalTide.Util;

import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_SENSOR_B;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSOR_BService;
import com.jhhc.RHDB.Data.Pojo.Data;
import com.jhhc.RHDB.Data.Pojo.MonthData;
import com.jhhc.RHDB.Data.Pojo.ST_TIDE_R;
import com.jhhc.RHDB.Data.Service.ST_TIDE_RService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.*;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_ST_ASTROTD_FService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class GetReportUitl {
    @Autowired
    private ST_TIDE_RService st_tide_rService;
    @Autowired
    private GetHighAndLowTides getHIghAndLowTides;
    @Autowired
    private BDMS_ST_SENSOR_BService bdms_st_sensor_bService;
    @Autowired
    private BDMS_ST_ASTROTD_FService bdms_st_astrotd_fService;


    public  Object getMeasuredTideMonthReport(String stcd,String  month) {
        List<String> stcds1 = new ArrayList<>();
        stcds1.add(stcd);
        //获取取数码
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_b=   bdms_st_sensor_bService.selectByStcds(stcds1);
        List<Data> maxTdzDatas = new ArrayList<>();
        //创建站码集合变量
        List<String> stcds = new ArrayList<>();
        if (bdms_st_sensor_b!=null&&bdms_st_sensor_b.size()>0) {
            stcds.add(bdms_st_sensor_b.get(0).getDatacode());
            //构造日期
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = new GregorianCalendar();
            //开始和结束日期
            String startTime = null;
            String endTime = null;
            //返回数据结构对象
            List<TidalMonthlyData> tidalMonthlyDatas = new ArrayList<>();
            TidalMonthly tidalMonthly = new TidalMonthly();
            tidalMonthly.setStationCode(stcd);
            //设置日期
            try {
                startTime = month + "-01 00:00:00";
                Date end = format.parse(startTime);
                calendar.setTime(end);
                calendar.add(Calendar.MONTH, +1);
                endTime = format.format(calendar.getTime());

            } catch (Exception e) {
                e.printStackTrace();
            }
            //获取实测数据
            List<ST_TIDE_R> st_tide_rs = st_tide_rService.selectByStcd(stcds, startTime, endTime);
            //获取高低潮
            List<HighAndLowTides> highAndLowTides = getHIghAndLowTides.getMeasuredHIghAndLowTides(st_tide_rs);
            for (HighAndLowTides highAndLowTide : highAndLowTides) {
                if (highAndLowTide.getHighAndLowTidesData() != null) {
                    if (highAndLowTide.getHighAndLowTidesData().getHigherHighTide() == null) {
                        HighAndLowTidesDatas data = new HighAndLowTidesDatas();
                        highAndLowTide.getHighAndLowTidesData().setHigherHighTide(data);
                    }
                    if (highAndLowTide.getHighAndLowTidesData().getLowHighTide() == null) {
                        HighAndLowTidesDatas data = new HighAndLowTidesDatas();
                        highAndLowTide.getHighAndLowTidesData().setLowHighTide(data);
                    }
                    if (highAndLowTide.getHighAndLowTidesData().getHighAndLowTide() == null) {
                        HighAndLowTidesDatas data = new HighAndLowTidesDatas();
                        highAndLowTide.getHighAndLowTidesData().setHighAndLowTide(data);
                    }
                    if (highAndLowTide.getHighAndLowTidesData().getLowerLowTide() == null) {
                        HighAndLowTidesDatas data = new HighAndLowTidesDatas();
                        highAndLowTide.getHighAndLowTidesData().setLowerLowTide(data);
                    }
                } else { //highAndLowTidesData为空set空对象
                    HighAndLowTidesDatas data = new HighAndLowTidesDatas();
                    HighAndLowTidesData highAndLowTidesData = new HighAndLowTidesData();
                    highAndLowTidesData.setHigherHighTide(data);
                    highAndLowTidesData.setLowHighTide(data);
                    highAndLowTidesData.setHighAndLowTide(data);
                    highAndLowTidesData.setLowerLowTide(data);
                    highAndLowTide.setHighAndLowTidesData(highAndLowTidesData);
                }
            }
            tidalMonthly.setHighAndLowTides(highAndLowTides);
            //获取数据后分组
            //根据小时分组
            Map<String, List<ST_TIDE_R>> DayListGroup = st_tide_rs.stream().collect(Collectors.groupingBy(ST_TIDE_R::getMonth2, Collectors.toList()));
            for (Map.Entry<String, List<ST_TIDE_R>> List : DayListGroup.entrySet()) {
                TidalMonthlyData tidalMonthlyData = new TidalMonthlyData();
                List<Data> datas = new ArrayList<>();
                String re = List.getKey().substring(3, 5);
                //tide.setStationCode(List.getKey());
                if (re.equals("00")) {
                    tidalMonthlyData.setHour(List.getKey());
                    for (int i = 0; i < List.getValue().size(); i++) {
                        if (List.getValue().get(i).getTdz() != null) {
                            Data data = new Data(List.getValue().get(i).getDay(), List.getValue().get(i).getTdz());
                            datas.add(data);
                        }
                    }
                    tidalMonthlyData.setDatas(datas);
                    if (tidalMonthlyData.getDatas() != null) {
                        tidalMonthlyData.getDatas().sort(Comparator.comparing(Data::getTM));
                    }
                    tidalMonthlyDatas.add(tidalMonthlyData);
                }
            }
            tidalMonthly.setTidalMonthlyDatas(tidalMonthlyDatas);
            System.out.println(tidalMonthlyDatas.size());
            if (tidalMonthlyDatas.size() > 0) {
                tidalMonthly.getTidalMonthlyDatas().sort(Comparator.comparing(TidalMonthlyData::getHour));
            } else {

                List<ST_TIDE_R> bdms_st_astrotd_f = st_tide_rService.selectNewEstData(stcds);
                if (bdms_st_astrotd_f.size() > 0) {
                    String dataMonth = bdms_st_astrotd_f.get(0).getTm().substring(0, 7);
                    tidalMonthly.setDataMonth(dataMonth);
                }

            }
            //获取每天最高值
            Map<String, List<ST_TIDE_R>> ListGroup = st_tide_rs.stream().collect(Collectors.groupingBy(ST_TIDE_R::getDay, Collectors.toList()));
            for (Map.Entry<String, List<ST_TIDE_R>> List : ListGroup.entrySet()) {
                Data maxData = new Data();
                ST_TIDE_R st_tide_r = List.getValue().stream().max(Comparator.comparing(ST_TIDE_R::getStcd)).get();

                if (st_tide_r != null) {
                    maxData.setTM(st_tide_r.getTm());
                    maxData.setValue(st_tide_r.getTdz());
                }
                maxTdzDatas.add(maxData);
            }

            tidalMonthly.setMaxTdzDatas(maxTdzDatas);
            tidalMonthly.getMaxTdzDatas().sort(Comparator.comparing(Data::getTM));
            return   tidalMonthly;
        }
      return "未查询到此站点";
    }




}
